<template>
  <view class="container">
    <!-- 顶部标题栏 -->
    <view class="header">
      <view class="header-title">卡密激活</view>
      <view class="user-status">当前等级：{{ userLevel }}</view>
    </view>
    
    <!-- 会员卡区域 -->
    <view class="card-section">
      <view class="card-container">
        <view class="card-title">会员等级</view>
        <view class="card-level">{{ userLevel }}</view>
        <button class="activate-btn" @click="openModal" v-if="isVip==='0'">
          <!-- 替换 fas fa-key 为内联 SVG -->
          <svg class="icon" viewBox="0 0 512 512" width="18" height="18">
            <path fill="currentColor" d="M336 224h-16v-64c0-52.937-43.063-96-96-96S128 107.063 128 160v64h-16c-13.255 0-24 10.745-24 24v192c0 13.255 10.745 24 24 24h224c13.255 0 24-10.745 24-24V248c0-13.255-10.745-24-24-24zm-160 0h128v-64c0-26.467-21.533-48-48-48s-48 21.533-48 48v64z"/>
          </svg>
          <span style="margin-left: 8px;">卡密激活</span>
        </button>
      </view>
    </view>
    
    <!-- 内容区域 -->
    <view class="content-section">
      <view class="section">
        <view class="section-title">
          <!-- 替换 fas fa-crown 为内联 SVG -->
          <svg class="icon" viewBox="0 0 512 512" width="22" height="22">
            <path fill="currentColor" d="M256 56c11.05 0 20 8.95 20 20v48h48c11.05 0 20 8.95 20 20v128c0 11.05-8.95 20-20 20h-24v112c0 26.51-21.49 48-48 48s-48-21.49-48-48V304h-24c-11.05 0-20-8.95-20-20V144c0-11.05 8.95-20 20-20h48V76c0-11.05 8.95-20 20-20zm-96 144h192v128h-24v96c0 8.82-7.18 16-16 16s-16-7.18-16-16v-96h-64v96c0 8.82-7.18 16-16 16s-16-7.18-16-16v-96h-24V200z"/>
          </svg>
          <span style="margin-left: 10px;">1. 会员介绍</span>
        </view>
        <view class="section-content">
          <span>抖工队学堂，每天有新的学习资料。你再也不用花几百去报名学习，不用担心被骗，学不到东西。</span>
          
          <view class="highlight">
            <span>资源在百度网盘上，小程序不能在线看。每个课程都有单独网盘分享链接。通过链接把资源保存到你的网盘上进行学习。</span>
          </view>
          
          <view class="benefits">
            <view class="benefit-item">
              <!-- 保留原 SVG 图标（无需替换） -->
              <svg class="icon" viewBox="0 0 512 512" width="32" height="32">
                <path fill="currentColor" d="M448 368V272c0-26.51-21.49-48-48-48h-48V112c0-26.51-21.49-48-48-48H160c-26.51 0-48 21.49-48 48v112H48c-26.51 0-48 21.49-48 48v96c0 26.51 21.49 48 48 48h16v112c0 26.51 21.49 48 48 48h288c26.51 0 48-21.49 48-48V416h16c26.51 0 48-21.49 48-48zM176 112c0-8.822 7.178-16 16-16h128c8.822 0 16 7.178 16 16v112H176V112zm16 48h128v32H192V160zm208 256c0 8.822-7.178 16-16 16H64c-8.822 0-16-7.178-16-16v-96h384v96zm0-128H48v-32h384V288z"/>
              </svg>
              <span>海量课程</span>
            </view>
            <view class="benefit-item">
              <!-- 保留原 SVG 图标 -->
              <svg class="icon" viewBox="0 0 512 512" width="32" height="32">
                <path fill="currentColor" d="M440.65 12.57l4 82.77A247.16 247.16 0 01456 128c0 136.66-110.57 247.67-247.25 248.05V384h32v64H104v-64h32v-39.69C73.22 325.11 32 272.22 32 216c0-106.39 85.56-192.08 191.53-192l2.81-4.79c4.19-7.2 12.68-10.45 20.67-7.76l82.77 40.33c7.2 3.42 10.45 11.91 7.76 20.67l-4.79 82.77c-3.42 7.2-11.91 10.45-20.67 7.76L320 195.47c-76.72 10.64-136 78.88-136 157.53 0 88.22 71.78 160 160 160s160-71.78 160-160c0-78.65-59.28-146.89-136-157.53l-19.13 3.09c-8.76 1.41-15.44 8.85-16.86 17.61l-4 82.77c-1.44 8.96 5.05 17.27 14.01 18.71l82.77 4c8.96 1.44 17.27-5.05 18.71-14.01l4-82.77c1.44-8.96-5.05-17.27-14.01-18.71l-82.77-4c-8.8-1.42-16.2 7.1-17.61 15.96l-3.09 19.13C382.35 151.21 432 203.57 432 264c0 114.87-93.13 208-208 208S16 378.87 16 264 109.13 56 224 56c47.86 0 90.45 19.24 123.64 50.44l19.13-3.09c8.76-1.41 16.2-8.85 17.61-17.61l4-82.77c1.44-8.96-5.05-17.27-14.01-18.71L367.41 12.5c-8.88-1.45-17.13 5.3-18.76 14.07z"/>
              </svg>
              <span>每日更新</span>
            </view>
            <view class="benefit-item">
              <!-- 保留原 SVG 图标 -->
              <svg class="icon" viewBox="0 0 512 512" width="32" height="32">
                <path fill="currentColor" d="M256 48C141.1 48 48 141.1 48 256s93.1 208 208 208 208-93.1 208-208S370.9 48 256 48zm0 352c-79.5 0-144-64.5-144-144s64.5-144 144-144 144 64.5 144 144-64.5 144-144 144zm-80-184c0-8.84 7.16-16 16-16h128c8.84 0 16 7.16 16 16v16c0 8.84-7.16 16-16 16H192c-8.84 0-16-7.16-16-16v-16z"/>
              </svg>
              <span>安全可靠</span>
            </view>
          </view>
        </view>
      </view>
      
      <view class="section">
        <view class="section-title">
          <!-- 替换 fas fa-shopping-cart 为内联 SVG -->
          <svg class="icon" viewBox="0 0 576 512" width="22" height="22">
            <path fill="currentColor" d="M528.12 301.319l47.273-208C578.806 78.301 567.391 64 551.99 64H159.208l-9.166-44.81C147.758 8.022 137.93 0 126.529 0H24C10.745 0 0 10.745 0 24v16c0 13.255 10.745 24 24 24h69.883l70.248 343.435C147.325 417.1 136 435.222 136 456c0 30.928 25.072 56 56 56s56-25.072 56-56c0-15.674-6.447-29.835-16.824-40h209.647C430.447 426.165 424 440.326 424 456c0 30.928 25.072 56 56 56s56-25.072 56-56c0-22.172-12.888-41.332-31.579-50.405l5.517-24.276c3.413-15.018-8.002-28.403-23.403-28.403H218.117l-6.545-32h293.145c11.206 0 20.92-7.754 23.403-18.681zM432 456c0 8.837-7.163 16-16 16s-16-7.163-16-16 7.163-16 16-16 16 7.163 16 16zm-80 0c0 8.837-7.163 16-16 16s-16-7.163-16-16 7.163-16 16-16 16 7.163 16 16zm-80 0c0 8.837-7.163 16-16 16s-16-7.163-16-16 7.163-16 16-16 16 7.163 16 16z"/>
          </svg>
          <span style="margin-left: 10px;">2. 开通流程</span>
        </view>
        <view class="section-content">
          <span>扫下面微信直接去购买卡密就可以</span>
          
          <view class="qr-code">
            <image
              v-if="!isLoadingQrcode && wechatQrcodeUrl"
              :src="wechatQrcodeUrl" 
              mode="widthFix" 
              class="qrcode-img"
              alt="微信客服二维码"
            ></image>
            <span>扫码购买会员卡密</span>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 卡密激活弹窗 -->
    <view class="modal" :class="{active: isModalActive}">
      <view class="modal-content">
        <view class="modal-close" @click="closeModal">
          <!-- 替换 fas fa-times 为内联 SVG -->
          <svg class="icon" viewBox="0 0 320 512" width="24" height="24">
            <path fill="currentColor" d="M310.6 150.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L160 210.7 54.6 105.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L114.7 256 9.4 361.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L160 301.3 265.4 406.6c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L205.3 256 310.6 150.6z"/>
          </svg>
        </view>
        <view class="modal-title">卡密激活</view>
        
        <view class="card-input-group">
          <input 
            type="text" 
            class="card-input" 
            v-model="cardKey"
            placeholder="请输入您的卡密" 
            maxlength="24"
            @focus="clearStatus"
          >
        </view>
        
        <button 
          class="modal-button submit-btn" 
          @click="submitCard"
          :disabled="isSubmitting"
        >
          <!-- 替换 fas fa-spinner 为内联 SVG（加载中） -->
          <svg class="icon" viewBox="0 0 512 512" width="18" height="18" v-if="isSubmitting" style="animation: spin 1s linear infinite;">
            <path fill="currentColor" d="M288 39.056v16.632a144.02 144.02 0 0 1 76.124 122.208 144.02 144.02 0 0 1-76.124 122.208V472.944a160.022 160.022 0 0 0 86.062 138.608A160.022 160.022 0 0 0 448 472.944V139.056a160.022 160.022 0 0 0-86.062-138.608A160.022 160.022 0 0 0 288 39.056zM224 472.944V456.312a144.02 144.02 0 0 1-76.124-122.208 144.02 144.02 0 0 1 76.124-122.208V39.056a160.022 160.022 0 0 0-86.062 138.608A160.022 160.022 0 0 0 64 472.944v16.632a160.022 160.022 0 0 0 86.062 138.608A160.022 160.022 0 0 0 224 472.944z"/>
          </svg>
          <!-- 替换 fas fa-check-circle 为内联 SVG（正常状态） -->
          <svg class="icon" viewBox="0 0 512 512" width="18" height="18" v-else>
            <path fill="currentColor" d="M504 256c0 136.967-111.033 248-248 248S8 392.967 8 256 119.033 8 256 8s248 111.033 248 248zM227.314 387.314l184-184c6.248-6.248 6.248-16.379 0-22.627l-22.627-22.627c-6.248-6.249-16.379-6.249-22.628 0L216 308.118l-70.059-70.059c-6.248-6.248-16.379-6.248-22.628 0l-22.627 22.627c-6.248 6.248-6.248 16.379 0 22.627l104 104c6.249 6.249 16.379 6.249 22.628.001z"/>
          </svg>
          <span v-if="isSubmitting" style="margin-left: 8px;"> 激活中...</span>
          <span v-else style="margin-left: 8px;"> 确认激活</span>
        </button>
        
        <button class="modal-button cancel-btn" @click="closeModal">
          <!-- 替换 fas fa-times 为内联 SVG -->
          <svg class="icon" viewBox="0 0 320 512" width="18" height="18">
            <path fill="currentColor" d="M310.6 150.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L160 210.7 54.6 105.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L114.7 256 9.4 361.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L160 301.3 265.4 406.6c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L205.3 256 310.6 150.6z"/>
          </svg>
          <span style="margin-left: 8px;"> 取消</span>
        </button>
        
        <view class="status-message" :class="statusType" v-if="showStatus">
          {{ statusMessage }}
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { checkcode } from '@/api/activation'
import { getIsVip } from '@/api/login'
import { getWechatServiceQrcode } from '@/api/wechat.js'
export default {
  data() {
    return {
      wechatQrcodeUrl:'',
      isVip:'',
      userLevel: '普通用户',
      activeTab: 2,
      isModalActive: false,
      cardKey: '',
      isSubmitting: false,
      showStatus: false,
      statusMessage: '',
      statusType: '',
      isLoadingQrcode: false // 补充缺失的 isLoadingQrcode 定义
    };
  },
  mounted() {
    this.getVip();
    this.loadWechatQrcode();
  },
  methods: {
    // 加载微信客服二维码
    async loadWechatQrcode() {
      try {
        const response = await getWechatServiceQrcode("客服");
        if (response.code === 200 && response.data) {
          this.wechatQrcodeUrl = response.data;
        } else {
          this.$modal?.showToast('获取二维码失败'); // 增加安全调用，避免 $modal 不存在报错
          this.wechatQrcodeUrl = '';
        }
      } catch (error) {
        console.error('获取微信二维码失败:', error);
        this.$modal?.showToast('网络错误，获取二维码失败');
        this.wechatQrcodeUrl = '';
      } finally {
        this.isLoadingQrcode = false;
      }
    },
    
    async getVip() {
      try {
        const res = await getIsVip(); 
        console.log(res); 
        if (res.code === 200) {
          this.isVip = res.data; 
          console.log('赋值成功:', this.isVip);
          if(this.isVip === "1"){
            this.userLevel = 'VIP会员';
            console.log("this.userLevel",this.userLevel)
          }
        } else {
          console.error('获取VIP状态失败:', res.msg);
        }
      } catch (error) {
        console.error('请求出错:', error);
      }
    },
    
    // 打开激活弹窗
    openModal() {
      this.isModalActive = true;
      this.cardKey = '';
      this.clearStatus();
    },
    
    // 关闭弹窗
    closeModal() {
      this.isModalActive = false;
      setTimeout(() => {
        this.clearStatus();
        this.cardKey = '';
      }, 300);
    },
    
    // 清除状态消息
    clearStatus() {
      this.showStatus = false;
      this.statusMessage = '';
      this.statusType = '';
    },
    
    submitCard() {
      // 防止重复提交
      if (this.isSubmitting) return;
      
      const cardKey = this.cardKey.trim();
      
      // 非空校验
      if (!cardKey) {
        this.showStatusMessage('请输入卡密', 'error');
        return;
      }
      
      this.isSubmitting = true;
      
      checkcode(cardKey).then(res => {
        if (res.code === 200) {
          this.showStatusMessage('激活成功！会员权益已开通', 'success');
          this.userLevel = res.data?.level || 'VIP会员'; // 增加安全调用
          
          setTimeout(() => {
            this.closeModal();
            this.isSubmitting = false;
            
            // 显示成功提示
            uni.showToast({
              title: '激活成功',
              icon: 'success'
            });
          }, 1500);
        } else {
          const errorMsg = res.msg || '卡密无效或已使用';
          this.showStatusMessage(`激活失败：${errorMsg}`, 'error');
          this.isSubmitting = false;
        }
      }).catch(err => {
        console.error('激活请求失败', err);
        const errorMsg = err.errMsg || '验证失败，请查看您的卡密是否正确';
        this.showStatusMessage(`激活失败：${errorMsg}`, 'error');
        this.isSubmitting = false;
      });
    },
    
    // 显示状态消息
    showStatusMessage(message, type) {
      this.showStatus = true;
      this.statusMessage = message;
      this.statusType = type;
    }
  }
};
</script>

<style scoped>
/* 删除远程引入代码：@import url(https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css); */

/* 新增 SVG 图标基础样式（确保图标颜色/大小适配） */
.icon {
  display: inline-block;
  vertical-align: middle;
  fill: currentColor; /* 继承父元素颜色，保持原样式一致性 */
}

/* 新增加载动画（对应 fas fa-spinner 的 spin 效果） */
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.container {
  width: 100%;
  max-width: 480px;
  background: #fff;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
  position: relative;
  margin: 0 auto;
}

/* 顶部标题栏 */
.header {
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  color: white;
  padding: 25px 20px;
  text-align: center;
  position: relative;
  border-bottom-left-radius: 20px;
  border-bottom-right-radius: 20px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.header::after {
  content: '';
  position: absolute;
  bottom: -20px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 40px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 -10px 10px rgba(0, 0, 0, 0.05);
}

.header-title {
  font-size: 24px;
  font-weight: 600;
  margin: 10px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.user-status {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  padding: 8px 15px;
  display: inline-block;
  font-size: 14px;
  margin-top: 10px;
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.15);
}

/* 会员卡区域 */
.card-section {
  padding: 35px 20px 25px;
  position: relative;
  z-index: 2;
}

.card-container {
  background: linear-gradient(135deg, #1a2980 0%, #26d0ce 100%);
  border-radius: 20px;
  padding: 30px 25px;
  color: white;
  position: relative;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  transform: translateY(0);
  transition: transform 0.3s ease;
}

.card-container:hover {
  transform: translateY(-5px);
}

.card-container::before {
  content: "";
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.15) 0%, transparent 70%);
  transform: rotate(30deg);
}

.card-title {
  font-size: 18px;
  margin-bottom: 5px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  display: block;
}

.card-level {
  font-size: 32px;
  font-weight: bold;
  margin: 15px 0;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  display: block;
}

.activate-btn {
  background: #ffdd00;
  color: #333;
  border: none;
  padding: 14px 35px;
  border-radius: 30px;
  font-size: 18px;
  font-weight: bold;
  display: block;
  margin: 20px auto 0;
  width: 80%;
  max-width: 240px;
  box-shadow: 0 6px 15px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  z-index: 2;
}

.activate-btn:hover {
  background: #ffc107;
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
}

.activate-btn:active {
  transform: translateY(0);
}

/* 内容区域 */
.content-section {
  padding: 0 20px 30px;
}

.section {
  background: white;
  border-radius: 18px;
  padding: 25px;
  margin-bottom: 25px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
  border: 1px solid #f0f0f0;
  transition: transform 0.3s ease;
}

.section:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
}

.section-title {
  color: #ff4e50;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  padding-bottom: 12px;
  border-bottom: 2px solid #f5f5f5;
}

/* 移除原 i 标签样式，保留 SVG 图标适配 */
.section-content {
  color: #555;
  line-height: 1.7;
  font-size: 16px;
}

.section-content span {
  display: block;
  margin-bottom: 15px;
}

.qr-code {
  text-align: center;
  margin-top: 25px;
  padding: 15px;
  background: #f9f9ff;
  border-radius: 12px;
}

.qr-code image {
  width: 200px;
  height: 200px;
  border: 10px solid white;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  border-radius: 12px;
}

.qr-code span {
  margin-top: 15px;
  color: #ff4e50;
  font-weight: bold;
  font-size: 17px;
  display: block;
}

.benefits {
  display: flex;
  justify-content: space-around;
  margin-top: 20px;
  flex-wrap: wrap;
}

.benefit-item {
  text-align: center;
  padding: 15px;
  flex: 1;
  min-width: 110px;
}

/* 移除原 i 标签样式，使用 SVG 图标样式 */
.benefit-item span {
  font-size: 15px;
  color: #444;
  font-weight: 500;
}

.highlight {
  background: #f0f9ff;
  border-left: 4px solid #2575fc;
  padding: 15px 20px;
  margin: 20px 0;
  border-radius: 0 10px 10px 0;
  font-size: 15px;
  color: #2c3e50;
}

/* 弹窗样式 */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
}

.modal.active {
  opacity: 1;
  visibility: visible;
}

.modal-content {
  background: white;
  border-radius: 20px;
  width: 90%;
  max-width: 400px;
  padding: 30px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
  transform: translateY(50px);
  transition: transform 0.4s ease;
  position: relative;
}

.modal.active .modal-content {
  transform: translateY(0);
}

.modal-close {
  position: absolute;
  top: 15px;
  right: 20px;
  font-size: 24px;
  color: #999;
  cursor: pointer;
  transition: color 0.3s;
}

.modal-close:hover {
  color: #ff4e50;
}

.modal-title {
  text-align: center;
  font-size: 24px;
  color: #2c3e50;
  margin-bottom: 25px;
  font-weight: 600;
}

.card-input-group {
  margin-bottom: 25px;
}

.card-input {
  width: 100%;
  padding: 16px 20px;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s;
  box-sizing: border-box;
  min-height: 56px;
}

.card-input:focus {
  border-color: #2575fc;
  box-shadow: 0 0 0 3px rgba(37, 117, 252, 0.2);
  outline: none;
}

.modal-button {
  display: block;
  width: 100%;
  padding: 16px;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  text-align: center;
}

.submit-btn {
  background: linear-gradient(to right, #6a11cb, #2575fc);
  color: white;
  margin-bottom: 15px;
  box-shadow: 0 5px 15px rgba(37, 117, 252, 0.4);
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 7px 20px rgba(37, 117, 252, 0.5);
}

.submit-btn:disabled {
  opacity: 0.8;
  transform: none;
  box-shadow: 0 5px 15px rgba(37, 117, 252, 0.4);
}

.cancel-btn {
  background: #f5f5f5;
  color: #666;
}

.cancel-btn:hover {
  background: #e0e0e0;
}

.status-message {
  text-align: center;
  padding: 15px;
  border-radius: 10px;
  margin-top: 20px;
  font-size: 16px;
}

.success {
  background: rgba(76, 175, 80, 0.1);
  color: #2e7d32;
}

.error {
  background: rgba(244, 67, 54, 0.1);
  color: #c62828;
}

/* 响应式调整 */
@media (max-width: 480px) {
  .header-title {
    font-size: 22px;
  }
  
  .card-level {
    font-size: 28px;
  }
  
  .benefit-item {
    min-width: 100px;
  }
  
  .section {
    padding: 20px;
  }
}
</style>